package com.sm.filemanagelibrary.adapter;

import android.view.View;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.scorpio.baselibrary.adapter.BasicMultiItemQuickAdapter;
import com.scorpio.baselibrary.adapter.BasicViewHolder;
import com.scorpio.baselibrary.utils.FileSizeUtil;
import com.scorpio.baselibrary.utils.glide.GlideUtil;
import com.scorpio.baselibrary.utils.TimeUtil;
import com.sm.filemanagelibrary.R;
import com.sm.filemanagelibrary.model.FileEntity;
import com.sm.filemanagelibrary.model.FolderEntity;
import com.sm.filemanagelibrary.util.FileLoadingUtil;
import com.sm.filemanagelibrary.util.FileTypeUtil;

import java.util.List;

/**
 * created by zhud on 2019/9/29
 */
public class FolderLiseAdapter extends BasicMultiItemQuickAdapter<MultiItemEntity, BasicViewHolder> {
    public static final int TYPE_FOLDER = 0;
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_IMAGE = 2;
    public static final int TYPE_DOC = 4;
    public static final int TYPE_APP = 5;
    public static final int TYPE_OTHERS = 6;

    public FolderLiseAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_FOLDER, R.layout.item_folder_manage_layout);
        addItemType(TYPE_VIDEO, R.layout.item_file_manage_layout);
        addItemType(TYPE_IMAGE, R.layout.item_file_image_layout);
        addItemType(TYPE_DOC, R.layout.item_file_manage_layout);
        addItemType(TYPE_APP, R.layout.item_file_manage_layout);
        addItemType(TYPE_OTHERS, R.layout.item_file_manage_layout);
    }

    @Override
    protected void convert(final BasicViewHolder helper, MultiItemEntity item) {
        FileEntity fileEntity = null;
        switch (helper.getItemViewType()) {
            case TYPE_FOLDER:
                final FolderEntity folderEntity = (FolderEntity) item;
                helper.setText(R.id.folder_item_text, FileLoadingUtil.AUDIO_NAME_KEY.equals(folderEntity.name) ? getString(R.string.file_string_00007) : folderEntity.name)
                        .setImageResource(R.id.folder_item_image, folderEntity.isExpanded() ? R.drawable.icon_arrows_open : R.drawable.icon_arrows_close);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (folderEntity.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_VIDEO:
            case TYPE_DOC:
            case TYPE_APP:
            case TYPE_OTHERS:
                fileEntity = (FileEntity) item;
                helper.setImageChecked(R.id.file_check, fileEntity.checked)
                        .setGone(R.id.file_check,true)
                        .setText(R.id.file_text, fileEntity.name)
                        .setGone(R.id.icon_rigth_image, fileEntity.isDirectory)
                        .setText(R.id.file_time, TimeUtil.formatMsecConvertTime(TimeUtil.TIME_PATTERN_FULL, fileEntity.lastModified))
                        .addOnClickListener(R.id.file_check)
                        .addOnClickListener(R.id.file_manage_item_layout);
                helper.setText(R.id.file_size, FileSizeUtil.FormetFileSize(fileEntity.size));
                int default1ImageRes = FileLoadingUtil.getFileImageRes(fileEntity.getType());
                if (helper.getItemViewType() == TYPE_APP) {
                    GlideUtil.loadAppIcon(mContext, FileTypeUtil.getPackageInfo(mContext, fileEntity.path), helper.getImageView(R.id.file_image), default1ImageRes, default1ImageRes);
                } else {
                    GlideUtil.load(mContext, fileEntity.path, helper.getImageView(R.id.file_image), default1ImageRes, default1ImageRes);
                }
                break;
            case TYPE_IMAGE:
                fileEntity = (FileEntity) item;
                int default2ImageRes = FileLoadingUtil.getFileImageRes(fileEntity.getType());
                helper.setImageChecked(R.id.file_check, fileEntity.checked)
                        .setGone(R.id.file_check,true)
                        .addOnClickListener(R.id.file_check)
                        .addOnClickListener(R.id.item_file_image);
                GlideUtil.load(mContext, fileEntity.path, helper.getImageView(R.id.item_file_image), default2ImageRes, default2ImageRes);
                break;
        }
    }
}
